package com.ruoyi.system.service.impl;

import java.util.List;
import com.ruoyi.common.core.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.core.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.system.domain.TbHouse;
import com.ruoyi.system.mapper.TbOwnerMapper;
import com.ruoyi.system.domain.TbOwner;
import com.ruoyi.system.service.ITbOwnerService;

/**
 * 房东管理Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-07-11
 */
@Service
public class TbOwnerServiceImpl implements ITbOwnerService 
{
    @Autowired
    private TbOwnerMapper tbOwnerMapper;

    /**
     * 查询房东管理
     * 
     * @param id 房东管理主键
     * @return 房东管理
     */
    @Override
    public TbOwner selectTbOwnerById(Long id)
    {
        return tbOwnerMapper.selectTbOwnerById(id);
    }

    /**
     * 查询房东管理列表
     * 
     * @param tbOwner 房东管理
     * @return 房东管理
     */
    @Override
    public List<TbOwner> selectTbOwnerList(TbOwner tbOwner)
    {
        return tbOwnerMapper.selectTbOwnerList(tbOwner);
    }

    /**
     * 新增房东管理
     * 
     * @param tbOwner 房东管理
     * @return 结果
     */
    @Transactional
    @Override
    public int insertTbOwner(TbOwner tbOwner)
    {
        tbOwner.setCreateTime(DateUtils.getNowDate());
        // 只保留images字段的第一个url
        if (tbOwner.getImages() != null && tbOwner.getImages().contains(",")) {
            tbOwner.setImages(tbOwner.getImages().split(",")[0]);
        }
        int rows = tbOwnerMapper.insertTbOwner(tbOwner);
        insertTbHouse(tbOwner);
        return rows;
    }

    /**
     * 修改房东管理
     * 
     * @param tbOwner 房东管理
     * @return 结果
     */
    @Transactional
    @Override
    public int updateTbOwner(TbOwner tbOwner)
    {
        tbOwner.setUpdateTime(DateUtils.getNowDate());
        // 只保留images字段的第一个url
        if (tbOwner.getImages() != null && tbOwner.getImages().contains(",")) {
            tbOwner.setImages(tbOwner.getImages().split(",")[0]);
        }
        tbOwnerMapper.deleteTbHouseByOwnerId(tbOwner.getId());
        insertTbHouse(tbOwner);
        return tbOwnerMapper.updateTbOwner(tbOwner);
    }

    /**
     * 批量删除房东管理
     * 
     * @param ids 需要删除的房东管理主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteTbOwnerByIds(Long[] ids)
    {
        tbOwnerMapper.deleteTbHouseByOwnerIds(ids);
        return tbOwnerMapper.deleteTbOwnerByIds(ids);
    }

    /**
     * 删除房东管理信息
     * 
     * @param id 房东管理主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteTbOwnerById(Long id)
    {
        tbOwnerMapper.deleteTbHouseByOwnerId(id);
        return tbOwnerMapper.deleteTbOwnerById(id);
    }

    /**
     * 新增房屋管理信息
     * 
     * @param tbOwner 房东管理对象
     */
    public void insertTbHouse(TbOwner tbOwner)
    {
        List<TbHouse> tbHouseList = tbOwner.getTbHouseList();
        Long id = tbOwner.getId();
        if (StringUtils.isNotNull(tbHouseList))
        {
            List<TbHouse> list = new ArrayList<TbHouse>();
            for (TbHouse tbHouse : tbHouseList)
            {
                tbHouse.setOwnerId(id);
                list.add(tbHouse);
            }
            if (list.size() > 0)
            {
                tbOwnerMapper.batchTbHouse(list);
            }
        }
    }
}
